<?php

namespace backend\controllers;

use common\models\Schemes;
use Yii;


class SchemeController extends DashboardController
{
    # 列表页
    public function actionIndex()
    {
        $data = Schemes::getAllSchemes([]);
        return $this->render('index',compact('data'));
    }

    # 添加页
    public function actionAdd()
    {
        $id = intval(Yii::$app->request->get('id',0));
        $detail = empty($id) ? [] : Schemes::find()->where(['id' => $id])->asArray()->one();

        return $this->render('add',compact('detail'));
    }

    # 保存方法
    public function actionSave()
    {
        $post = Yii::$app->request->post();
        $item = !empty($post['id']) ? Schemes::find()->where(['id' => $post['id']])->one() : new Schemes();

        $saveData = [
            'title' => !empty($post['title']) ? $post['title'] : '',
            'desc' => !empty($post['desc']) ? $post['desc'] : '',
            'picture' => !empty($post['picture']) ? $post['picture'] : '',
            'status' => !empty($post['desc']) ? $post['status'] : 0,
        ];
        $item->attributes = $saveData;
        if ($item->validate()){
            $msg = $item->save() ? ["state" =>"success","message" =>"恭喜小主，操作成功！"] : ["state" => "error","message" => "小主大人，操作失败啦！"];
            $this->ajaxReturn($msg);
        }else{
            $this->ajaxReturn(["state" => 'error',"message" => '验证失败']);
        }
    }

    # 删除
    public function actionDelete()
    {
        $id = intval(Yii::$app->request->post('id',0));

        if (empty($id)) {
            $this->ajaxReturn(['state' => 0,'message' => "参数错误，请求非法"]);
        }
        $model = Schemes::find()->where(['id' => $id])->one();

        if($model){
            $transaction = Yii::$app->db->beginTransaction();
            if($model->delete()){
                $transaction->commit();
                $this->ajaxReturn([
                    'state' => 1,
                    'message' => '删除成功！',
                ]);
            }

            $transaction->rollBack();
            $this->ajaxReturn([
                'state' => 0,
                'message' => '删除失败！'.implode('',array_flatten($model->getErrors())),
            ]);
        }else{
            $this->ajaxReturn([
                'state' => 0,
                'message' => '数据不存在！',
            ]);
        }
    }

    # 解决方案
    public function actionChange()
    {
        $id = Yii::$app->request->post('id');
        if (empty($id)) {
            $this->ajaxReturn(['state' => 0,'message' => "参数错误，请求非法"]);
        }
        $model = Schemes::find()->where(['id' => $id])->one();
        if ($model){

            $model->status = $model->status == 1 ? 0 : 1;
            if($model->save()){
                $this->ajaxReturn([
                    'state' => 1,
                    'message' => '切换成功！',
                ]);
            }
            $this->ajaxReturn([
                'state' => 0,
                'message' => '切换失败！'.implode('',array_flatten($model->getErrors())),
            ]);
        }else{
            $this->ajaxReturn([
                'state' => 0,
                'message' => '数据不存在！',
            ]);
        }
    }

}
